package com.demo.shiro;

import com.demo.shiro.service.ShiroService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class controller {
    @Autowired
    private ShiroService shiroService;
    @RequestMapping(value = "/shiroLogin",method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String login(String username,String password){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            token.setRememberMe(true);
            try {
                subject.login(token);
            }catch (UnknownAccountException e){
                System.out.println(e);
            }catch (LockedAccountException e){
                System.out.println(e);
            }
            catch (AuthenticationException e) {
                System.out.println("登录失败："+username);
            }
        }
       return "redirect:/list.jsp";
    }
    @RequestMapping(value = "/shiroAnnotation",method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public @ResponseBody String testAnnotation(HttpSession session){
        session.setAttribute("sessionKey","3333333333333");
        shiroService.testAnnotation();
        return  "测试注解";
    }
}
